﻿using System;
using System.Collections.Generic;
using System.Text;
using CFSystem.JXC.IDAL;
using CFSystem.JXC.Model;
using System.Data;
using System.Data.OleDb;
using DBUtility;

namespace CFSystem.JXC.AccessDAL
{
    public class ProdCatergory : IProdCatergory
    {


        public ProdCatergory()
        { }

        /// <summary>
        /// Method to get all BrandInfo
        /// </summary>		
        /// <returns>Interface to Model Collection Generic of Brands</returns>
        public IList<ProdCatergoryInfo> GetProdCatergorys()
        {

            string sqlSelect = "SELECT * from tb_TypeRelation";

            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            IList<ProdCatergoryInfo> ProdCategoryItems = new List<ProdCatergoryInfo>();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    ProdCatergoryInfo PCI = new ProdCatergoryInfo(Convert.ToInt32(dr["TypeID"].ToString()), Convert.ToString(dr["Name"].ToString()), Convert.ToString(dr["ParentName"]));
                    ProdCategoryItems.Add(PCI);
                }
                dr.Close();
            }
            return ProdCategoryItems;
        }

        /// <summary>
        /// Get information on a specific Brand
        /// </summary>
        /// <param name="categoryId">Unique identifier for a Brand</param>
        /// <returns>Business Entity representing an Brand</returns>
        public ProdCatergoryInfo GetProdCatergory(string ProdCatergoryID)
        {
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="BrandName"></param>
        public void InsertProdCatergory(string parentName, string name)
        {
            string sqlInsert = "insert into [tb_TypeRelation]([ParentName],[Name]) values('" + parentName + "','" + name + "')";

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlInsert);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="BrandID"></param>
        public void DelProdCatergoryByID(int TypeID)
        {
            string sqlDel = "delete from [tb_TypeRelation] where [TypeID]=" + TypeID;

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlDel);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="BrandID"></param>
        /// <param name="updValue"></param>
        public void UpdateProdCatergoryByID(int TypeID, string name, string parentName)
        {
            string sqlUpd = "update [tb_TypeRelation] set [Name] ='" + name + "'" + ",[ParentName]='" + parentName + "'" + " where [TypeID]=" + TypeID;

            int returnCnt = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, sqlUpd);

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="parentName"></param>
        public bool IsExists(string name, string parentName)
        {
            string sqlSelect = "SELECT * from tb_TypeRelation where Name='" + name + "'" + " and ParentName='" + parentName + "'";

            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            if (dr.HasRows)
            {
                dr.Close();
                return true;
            }
            else
            {
                dr.Close();
                return false;
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<string> GetAllCatergoryList()
        {
            string sqlSelect = "SELECT Name from tb_TypeRelation";

            OleDbDataReader dr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, sqlSelect);

            IList<string> ProdCategoryItems = new List<string>();

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    if (!ProdCategoryItems.Contains(dr["Name"].ToString()))
                    {
                        ProdCategoryItems.Add(dr["Name"].ToString());
                    }
                }
                dr.Close();
            }
            return ProdCategoryItems;
        }
    }
}
